#!/bin/bash
# 1. check uid entry existence
# 2. add memberUid attribute in cn=PAM,dc=jhc,dc=cn 
# 3. set gidNumber of uid entry to 9999
# 4. set right homeDirectory

uid=$1
binddn='cn=Manager,dc=jhc,dc=cn'
pwdfile=~/.ldappwd
basedn='dc=jhc,dc=cn'
#classcn=$1

ldapsearch -x -y "$pwdfile" -D "$binddn" -b "$basedn" -s sub "(uid=$uid)"  | grep '^# numEntries: 1'
if [ $? -eq 0 ] ; then
    echo "Find entry with uid=$uid."
else
    echo "Cannot find entry with uid=$uid."
    exit 1
fi

echo "Now we add uid to cn=PAM,dc=jhc,dc=cn as memberUid attribute."
echo -e -n "dn: cn=PAM,dc=jhc,dc=cn\nchangetype: modify\nadd: memberUid\nmemberUid: $uid\n" | ldapmodify -x -y "$pwdfile" -D "$binddn"

uiddn=$(ldapsearch -x -y "$pwdfile" -D "$binddn" -b "$basedn" -s sub "(uid=$uid)" | grep '^dn: uid=' | sed 's/^dn: //')
echo "Now we change gidNumber of $uiddn to 9999, add homeDirectory and loginShell"
echo -e -n "dn: $uiddn\nchangetype: modify\nreplace: gidNumber\ngidNumber: 9999\n-\nreplace: homeDirectory\nhomeDirectory: /home/$uid\n-\nreplace: loginShell\nloginShell: /bin/bash\n" | ldapmodify -x -y "$pwdfile" -D "$binddn"



